package carassius.GUI;

import carassius.DAL.AnimalRow;
import carassius.DAL.Database;
import carassius.DAL.OrganismListRow;
import java.awt.Window;
import java.io.File;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 *
 * @author siebz0r
 */
public class AnimalInfoSmallPanel
		extends javax.swing.JPanel
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private AnimalRow _animalRow;
	private OrganismListRow _organismListRow;
	private final Window _parentWindow;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public AnimalInfoSmallPanel(Window parent)
	{
		_parentWindow = parent;
		initComponents();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public void setAnimalRow(AnimalRow animalRow)
	{
		_animalRow = animalRow;
		uxAmountPanel.setVisible(false);

		if (_animalRow.getAlternativeName() != null && !_animalRow.getAlternativeName().isEmpty())
		{
			uxAlternativeNamePanel.setVisible(true);
			uxAlternativeNameLabel.setText(_animalRow.getAlternativeName());
		}
		else
		{
			uxAlternativeNamePanel.setVisible(false);
		}

		if (_animalRow.getScientificName() != null && !_animalRow.getScientificName().isEmpty())
		{
			uxScientificNamePanel.setVisible(true);
			uxScientificNameLabel.setText(_animalRow.getScientificName());
		}
		else
		{
			uxScientificNamePanel.setVisible(false);
		}

		if (_animalRow.getMaxSize() == null)
		{
			uxLengthPanel.setVisible(false);
		}
		else
		{
			uxLengthPanel.setVisible(true);
			uxLengthLabel.setText(Double.toString(_animalRow.getMaxSize()));
		}

		File pic = null;
		if (_animalRow.getPicture() != null)
		{
			pic = new File(_animalRow.getPicture().getPath());
			if (!pic.exists())
			{
				pic = null;
			}
		}
		uxPicturePanel.setImagePath(pic);
	}

	public void setOrganismListRow(OrganismListRow organismListRow)
	{
		_organismListRow = organismListRow;
		try
		{
			setAnimalRow((AnimalRow) Database.getAnimalTable().getByPrimaryKey(organismListRow.getOrganismId()));
		}
		catch (SQLException ex)
		{
			System.out.println(ex.toString());
		}
		uxAmountSpinner.setValue(organismListRow.getAmount());
		uxAmountPanel.setVisible(true);
	}

	public AnimalRow getAnimalRow()
	{
		return _animalRow;
	}

	public void setAmount(int amount)
	{
		uxAmountSpinner.setValue(amount);
	}

	public OrganismListRow getOrganismListRow()
	{
		return _organismListRow;
	}
	//</editor-fold>

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        uxPicturePanel = new carassius.GUI.PicturePanel(null);
        uxInfoPanel = new javax.swing.JPanel();
        uxScientificNamePanel = new javax.swing.JPanel();
        uxScientificNamePrefixLabel = new javax.swing.JLabel();
        uxScientificNameLabel = new javax.swing.JLabel();
        uxAlternativeNamePanel = new javax.swing.JPanel();
        uxAlternativeNamePrefixLabel = new javax.swing.JLabel();
        uxAlternativeNameLabel = new javax.swing.JLabel();
        uxAmountPanel = new javax.swing.JPanel();
        uxAmountPrefixLabel = new javax.swing.JLabel();
        uxAmountSpinner = new javax.swing.JSpinner();
        uxLengthPanel = new javax.swing.JPanel();
        uxLengthPrefixLabel = new javax.swing.JLabel();
        uxLengthLabel = new javax.swing.JLabel();
        uxLengthPostfixLabel = new javax.swing.JLabel();
        uxButtonPanel = new javax.swing.JPanel();
        uxEditButton = new javax.swing.JButton();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setMaximumSize(new java.awt.Dimension(2147483647, 160));
        setMinimumSize(new java.awt.Dimension(382, 160));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(382, 160));
        setLayout(new java.awt.GridBagLayout());

        uxPicturePanel.setMaximumSize(new java.awt.Dimension(150, 150));
        uxPicturePanel.setMinimumSize(new java.awt.Dimension(150, 150));
        uxPicturePanel.setName("uxPicturePanel"); // NOI18N
        uxPicturePanel.setPreferredSize(new java.awt.Dimension(150, 150));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${background}"), uxPicturePanel, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${foreground}"), uxPicturePanel, org.jdesktop.beansbinding.BeanProperty.create("foreground"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout uxPicturePanelLayout = new javax.swing.GroupLayout(uxPicturePanel);
        uxPicturePanel.setLayout(uxPicturePanelLayout);
        uxPicturePanelLayout.setHorizontalGroup(
            uxPicturePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        uxPicturePanelLayout.setVerticalGroup(
            uxPicturePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(uxPicturePanel, gridBagConstraints);

        uxInfoPanel.setName("uxInfoPanel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${background}"), uxInfoPanel, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${foreground}"), uxInfoPanel, org.jdesktop.beansbinding.BeanProperty.create("foreground"));
        bindingGroup.addBinding(binding);

        uxInfoPanel.setLayout(new java.awt.GridBagLayout());

        uxScientificNamePanel.setName("uxScientificNamePanel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${background}"), uxScientificNamePanel, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${foreground}"), uxScientificNamePanel, org.jdesktop.beansbinding.BeanProperty.create("foreground"));
        bindingGroup.addBinding(binding);

        uxScientificNamePanel.setLayout(new javax.swing.BoxLayout(uxScientificNamePanel, javax.swing.BoxLayout.Y_AXIS));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(carassius.CarassiusApp.class).getContext().getResourceMap(AnimalInfoSmallPanel.class);
        uxScientificNamePrefixLabel.setText(resourceMap.getString("uxScientificNamePrefixLabel.text")); // NOI18N
        uxScientificNamePrefixLabel.setName("uxScientificNamePrefixLabel"); // NOI18N
        uxScientificNamePanel.add(uxScientificNamePrefixLabel);

        uxScientificNameLabel.setFont(resourceMap.getFont("uxScientificNameLabel.font")); // NOI18N
        uxScientificNameLabel.setText(resourceMap.getString("uxScientificNameLabel.text")); // NOI18N
        uxScientificNameLabel.setName("uxScientificNameLabel"); // NOI18N
        uxScientificNamePanel.add(uxScientificNameLabel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        uxInfoPanel.add(uxScientificNamePanel, gridBagConstraints);

        uxAlternativeNamePanel.setName("uxAlternativeNamePanel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${background}"), uxAlternativeNamePanel, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${foreground}"), uxAlternativeNamePanel, org.jdesktop.beansbinding.BeanProperty.create("foreground"));
        bindingGroup.addBinding(binding);

        uxAlternativeNamePanel.setLayout(new javax.swing.BoxLayout(uxAlternativeNamePanel, javax.swing.BoxLayout.Y_AXIS));

        uxAlternativeNamePrefixLabel.setText(resourceMap.getString("uxAlternativeNamePrefixLabel.text")); // NOI18N
        uxAlternativeNamePrefixLabel.setName("uxAlternativeNamePrefixLabel"); // NOI18N
        uxAlternativeNamePanel.add(uxAlternativeNamePrefixLabel);

        uxAlternativeNameLabel.setFont(resourceMap.getFont("uxAlternativeNameLabel.font")); // NOI18N
        uxAlternativeNameLabel.setText(resourceMap.getString("uxAlternativeNameLabel.text")); // NOI18N
        uxAlternativeNameLabel.setName("uxAlternativeNameLabel"); // NOI18N
        uxAlternativeNamePanel.add(uxAlternativeNameLabel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        uxInfoPanel.add(uxAlternativeNamePanel, gridBagConstraints);

        uxAmountPanel.setName("uxAmountPanel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${background}"), uxAmountPanel, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${foreground}"), uxAmountPanel, org.jdesktop.beansbinding.BeanProperty.create("foreground"));
        bindingGroup.addBinding(binding);

        uxAmountPanel.setLayout(new javax.swing.BoxLayout(uxAmountPanel, javax.swing.BoxLayout.LINE_AXIS));

        uxAmountPrefixLabel.setText(resourceMap.getString("uxAmountPrefixLabel.text")); // NOI18N
        uxAmountPrefixLabel.setName("uxAmountPrefixLabel"); // NOI18N
        uxAmountPanel.add(uxAmountPrefixLabel);

        uxAmountSpinner.setMaximumSize(new java.awt.Dimension(40, 20));
        uxAmountSpinner.setMinimumSize(new java.awt.Dimension(40, 20));
        uxAmountSpinner.setName("uxAmountSpinner"); // NOI18N
        uxAmountSpinner.setPreferredSize(new java.awt.Dimension(40, 20));
        uxAmountSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                uxAmountSpinnerStateChanged(evt);
            }
        });
        uxAmountPanel.add(uxAmountSpinner);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        uxInfoPanel.add(uxAmountPanel, gridBagConstraints);

        uxLengthPanel.setName("uxLengthPanel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${background}"), uxLengthPanel, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${foreground}"), uxLengthPanel, org.jdesktop.beansbinding.BeanProperty.create("foreground"));
        bindingGroup.addBinding(binding);

        uxLengthPanel.setLayout(new javax.swing.BoxLayout(uxLengthPanel, javax.swing.BoxLayout.X_AXIS));

        uxLengthPrefixLabel.setText(resourceMap.getString("uxLengthPrefixLabel.text")); // NOI18N
        uxLengthPrefixLabel.setName("uxLengthPrefixLabel"); // NOI18N
        uxLengthPanel.add(uxLengthPrefixLabel);

        uxLengthLabel.setFont(resourceMap.getFont("uxLengthLabel.font")); // NOI18N
        uxLengthLabel.setText(resourceMap.getString("uxLengthLabel.text")); // NOI18N
        uxLengthLabel.setName("uxLengthLabel"); // NOI18N
        uxLengthPanel.add(uxLengthLabel);

        uxLengthPostfixLabel.setText(resourceMap.getString("uxLengthPostfixLabel.text")); // NOI18N
        uxLengthPostfixLabel.setName("uxLengthPostfixLabel"); // NOI18N
        uxLengthPanel.add(uxLengthPostfixLabel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        uxInfoPanel.add(uxLengthPanel, gridBagConstraints);

        uxButtonPanel.setName("uxButtonPanel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${background}"), uxButtonPanel, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${foreground}"), uxButtonPanel, org.jdesktop.beansbinding.BeanProperty.create("foreground"));
        bindingGroup.addBinding(binding);

        uxButtonPanel.setLayout(new java.awt.GridBagLayout());

        uxEditButton.setText(resourceMap.getString("uxEditButton.text")); // NOI18N
        uxEditButton.setName("uxEditButton"); // NOI18N
        uxEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxEditButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        uxButtonPanel.add(uxEditButton, gridBagConstraints);

        filler2.setName("filler2"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        uxButtonPanel.add(filler2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        uxInfoPanel.add(uxButtonPanel, gridBagConstraints);

        filler1.setName("filler1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weighty = 1.0;
        uxInfoPanel.add(filler1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        add(uxInfoPanel, gridBagConstraints);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

	private void uxAmountSpinnerStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_uxAmountSpinnerStateChanged
	{//GEN-HEADEREND:event_uxAmountSpinnerStateChanged
		if (uxAmountSpinner.getValue() != null)
		{
			int spinner = (Integer) uxAmountSpinner.getValue();
			if (spinner != _organismListRow.getAmount())
			{
				if (spinner == 0)
				{
					switch (JOptionPane.showConfirmDialog(this,
							ResourceBundle.getBundle("carassius.resources.Messages").getString("RemoveAnimalFromAquarium"),
							ResourceBundle.getBundle("carassius.resources.Messages").getString("RemoveAnimalFromAquariumTitle"),
							JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE))
					{
						case JOptionPane.YES_OPTION:
							try
							{
								Database.getOrganismListTable().delete(_organismListRow);
							}
							catch (SQLException ex)
							{
								JOptionPane.showMessageDialog(this,
										ResourceBundle.getBundle("carassius.resources.Messages").getString("RemoveAnimalFromAquariumError"),
										ResourceBundle.getBundle("carassius.resources.Messages").getString("RemoveAnimalFromAquariumErrorTitle"),
										JOptionPane.ERROR_MESSAGE);
							}
							break;
						default:
							uxAmountSpinner.setValue(_organismListRow.getAmount());
							break;
					}
				}
				else if (!_organismListRow.setAmount((Integer) uxAmountSpinner.getValue()))
				{
					uxAmountSpinner.setValue(_organismListRow.getAmount());
				}
			}
		}
	}//GEN-LAST:event_uxAmountSpinnerStateChanged

	private void uxEditButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxEditButtonActionPerformed
	{//GEN-HEADEREND:event_uxEditButtonActionPerformed
		AnimalForm af = new AnimalForm(_parentWindow, _animalRow);
		af.setVisible(true);
	}//GEN-LAST:event_uxEditButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.JLabel uxAlternativeNameLabel;
    private javax.swing.JPanel uxAlternativeNamePanel;
    private javax.swing.JLabel uxAlternativeNamePrefixLabel;
    private javax.swing.JPanel uxAmountPanel;
    private javax.swing.JLabel uxAmountPrefixLabel;
    private javax.swing.JSpinner uxAmountSpinner;
    private javax.swing.JPanel uxButtonPanel;
    private javax.swing.JButton uxEditButton;
    private javax.swing.JPanel uxInfoPanel;
    private javax.swing.JLabel uxLengthLabel;
    private javax.swing.JPanel uxLengthPanel;
    private javax.swing.JLabel uxLengthPostfixLabel;
    private javax.swing.JLabel uxLengthPrefixLabel;
    private carassius.GUI.PicturePanel uxPicturePanel;
    private javax.swing.JLabel uxScientificNameLabel;
    private javax.swing.JPanel uxScientificNamePanel;
    private javax.swing.JLabel uxScientificNamePrefixLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}